//
//  TableViewController.m
//  MyProject
//
//  Created by yun on 2018/4/25.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "TableViewController.h"
#import "TempTableViewCell.h"
#import "TableViewCell.h"

@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [[NSMutableArray alloc] initWithArray:@[@"行走在非洲，内心一定是激动或者感慨万千的",
                                                             @"行走在非洲，\n内心一定是激动或者感慨万千的",
                                                             @"行走在非洲，内心一定是激动或者感慨万千的，当教科书上或者电影场景中的片段清晰的浮现在你的眼前时，一切都来的那么自然而然，那么经久不衰与念念不忘。",
                                                             @"行走在非洲，内心一定是激动或者感慨万千的，\n当教科书上或者电影场景中的片段清晰的浮现在你的眼前时，\n一切都来的那么自然而然，那么经久不衰与念念不忘。",
                                                             @"行走在非洲，\n内心一定是激动或者感慨万千的，\n当教科书上或者电影场景中的片段清晰的浮现在你的眼前时，\n一切都来的那么自然而然，那么经久不衰与念念不忘。",
                                                             @"行走在非洲，\n内心一定是激动或者感慨万千的，\n当教科书上或者电影场景中的片段清晰的浮现在你的眼前时，\n一切都来的那么自然而然，\n那么经久不衰与念念不忘。"]];
//    self.dataArray = [NSMutableArray new];
//    for (int i = 0; i <= 30; i++) {
//        [self.dataArray addObject:[NSString stringWithFormat:@"第%d行",i]];
//    }

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = kHexColor(0x3A83F7);
    [button setTitle:@"操作" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = kHexColor(0xCCCCCC);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // autolayout布局自动计算行高设置
//    self.tableView.estimatedRowHeight = 50;
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    UIImageView *bgImageV = [[UIImageView alloc] init];
    [bgImageV sd_setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524659354835&di=816872e2208e46e00626e9a3b179b1b4&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2Ff%2F5971c0a6f2119.png"]];
    self.tableView.backgroundView = bgImageV;
//    self.tableView.editing = YES;
    
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(button.mas_top);
    }];
    
    [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:cellIdentifier];
//    [self.tableView registerNib:[UINib nibWithNibName:@"TempTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    
}

- (void)buttonClick:(UIButton *)sender{
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
}

#pragma mark -- 代理
/*
 调用顺序：numberOfRowsInSection → estimatedHeight → cellForRow → heightForRow → willDisplayCell
 所以autolayout布局，自动设置行高的，数据源需要在 cellForRow 传入
 */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    /*
     accessoryType有信息按钮出现时：UITableViewCellAccessoryDetailDisclosureButton、UITableViewCellAccessoryDetailButton
     */
    NSLog(@"点击了");
}

/*
 将要显示cell是设置数据
 若是autolayout布局，自动设置行高的话，一定不能在这里设置数据，不然不会自动计算行高
 */
//- (void)tableView:(UITableView *)tableView willDisplayCell:(TableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    cell.cLabel.text = self.dataArray[indexPath.row];
//
//
//}

 // autolayout布局自动计算行高设置 神器，不用自己计算行高
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    NSLog(@"%ldcell从tableview移除",indexPath.row);
}

static NSString * const cellIdentifier = @"cellIdentifier";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//    cell.accessoryType = UITableViewCellAccessoryDetailButton;
//    cell.cLabel.text = self.dataArray[indexPath.row];
    // 自动计算行高必须再该方法里添加数据
    [cell setTitle:self.dataArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//    return YES;
//}






- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

// 删除、插入 数据及UI处理
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除啦";
}

// cell上的Focus事件，NO didSelectRowAtIndexPath不响应
//- (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath{
//    return NO;
//}

/* 复制、粘贴等操作
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        return YES;
    }
    return NO;
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
    if (action == @selector(copy:)) {
        
        return YES;
    }
    
    return NO;
    
}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    
    if (action == @selector(copy:)) {
        
        TableViewCell *messageCell = [tableView cellForRowAtIndexPath:indexPath];
        [UIPasteboard generalPasteboard].string = messageCell.titleLabel.text;
    }
    
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
